Mock.mock("/js/Mycar","get",function(){
    var arrmoney=[]
    function havenot(){
        var newtd=document.createElement('tr')
        newtd.innerHTML=`
        <td>暂无数据</td>
        <td>暂无数据</td>
        <td>暂无数据</td>
        <td>暂无数据</td>
    `
        var tableobj=document.getElementsByTagName('table')[0]
        document.getElementById('enddo').innerText=`购物车小计￥0`
        document.getElementById('akk').innerText=`订单总计￥0`
        tableobj.appendChild(newtd)
    }
    if(localStorage.ShopCar==undefined){
        havenot()
    }else{
        var bool=false
        var allshop=JSON.parse(localStorage.ShopCar)
       for (const iterator of allshop) {
            var allprice=0
            allprice+=parseInt(iterator.price)*parseInt(iterator.shuliang)
            bool=true
            var newtd=document.createElement('tr')
            arrmoney.push(iterator.price)
            newtd.innerHTML=`
            <td>
            <button style="width: 20px;height: 20px; float:left;margin-top:30px" onclick='deltea(${JSON.stringify(iterator)})'>x</button>
            <img src='${iterator.img}' alt="" style="width: 100px;height: 100px;">
            <h3>${iterator.name}</h3> 
            </td>
            <td>￥${iterator.price}</td>
            <td> <button id='one' style="width: 20px;height: 20px;">-</button>
                <input type='number' value='${iterator.shuliang}' style="width: 40px;">
                <button id='add' style="width: 20px;height: 20px;">+</button>
            </td>
            <td><p>￥${allprice}</p></td>
            `
        var tableobj=document.getElementsByTagName('table')[0]
        tableobj.appendChild(newtd)
        allone= document.querySelectorAll('#one')
        alladd= document.querySelectorAll('#add')
        allinp=document.getElementsByTagName('input')
    }
    checkallmoney()
    if(!bool){
        havenot()
    }       
}
allone= document.querySelectorAll('#one')
alladd= document.querySelectorAll('#add')
allinp=document.getElementsByTagName('input')
function checkallmoney(){
    var alltitle=0
    var allmoney=0
    for(i=0;i<allinp.length;i++){
        console.log(allinp[i].value);
        alltitle+=parseInt(allinp[i].value)
        allmoney+=parseInt(allinp[i].value)*arrmoney[i]
    }
    document.getElementById('enddo').innerText=`购物车小计￥${allmoney}`
    document.getElementById('akk').innerText=`订单总计￥${allmoney}`
}
for (var i = 0; i < allone.length; i++) {
    // 使用IIFE捕获当前的i值
    (function(currentIndex) {
        allone[currentIndex].onclick = function() {
            // 当点击事件触发时，currentIndex将是循环迭代时的i值
            allinpdo(currentIndex);
        };
        alladd[currentIndex].onclick = function() {
                // 当点击事件触发时，currentIndex将是循环迭代时的i值
            alladddo(currentIndex);
        };
        allinp[currentIndex].onblur = function() {
                // 当点击事件触发时，currentIndex将是循环迭代时的i值
            if(allinp[currentIndex].value<1){
                alert('不允许比1小')
                document.getElementsByTagName('input')[currentIndex].value=1
            }else{
                checkallmoney()
            }
        };
    })(i); // 立即执行函数，并将当前的i值作为参数传递

    }
return arrmoney
})

